package com.ztgf.shopping.service.shop;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.dto.BrandInfoAuditDTO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.service.IBrandInfoAuditService;
import com.ztgf.commodity.models.service.IBrandInfoService;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopInfoAuditMapper;
import com.ztgf.shopping.models.bo.shop.*;
import com.ztgf.shopping.models.dto.shop.*;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.shop.*;
import com.ztgf.shopping.models.vo.shop.*;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

//import com.codingapi.txlcn.tc.annotation.LcnTransaction;

/**
 * 店铺审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-06-23 11:56:32
 */
@DubboService
public class ShopInfoAuditServiceImpl extends ServiceImpl<ShopInfoAuditMapper, ShopInfoAuditDTO> implements IShopInfoAuditService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private IShopQualificationAuditService iShopQualificationAuditService;
    @DubboReference
    private IUserInfoService iUserInfoService;
    @DubboReference
    private IUserLoginService iUserLoginService;
    @Autowired
    private IShopInfoAuditLogService iShopInfoAuditLogService;
    @Autowired
    private IShopBrandRelationAuditService iShopBrandRelationAuditService;
    @DubboReference
    private IBrandInfoService iBrandInfoService;
    @DubboReference
    private IBrandInfoAuditService iBrandInfoAuditService;
    @Autowired
    private IShopCategoryRelationAuditService iShopCategoryRelationAuditService;
    @Autowired
    @Lazy
    private IShopInfoService iShopInfoService;
    @Autowired
    private IShopSalesAreaService shopSalesAreaService;
    @DubboReference
    private ICommodityCategoryService iCommodityCategoryService;


    @Override
    public boolean add(ShopInfoAuditSaveBO saveBO) {
        ShopInfoAuditDTO dto = new ShopInfoAuditDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Page<ShopInfoAuditDTO> queryLoginPageList(ShopInfoAuditBO bo) {
        Page<ShopInfoAuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<ShopInfoAuditDTO> shopInfoAuditDTOS = baseMapper.queryLoginPageList(page, bo);
        page.setRecords(shopInfoAuditDTOS);
        return page;
    }

    @Override
    public Integer queryLoginCount() {
        return baseMapper.queryLoginCount();
    }

    @Override
    public Page<ShopInfoAuditDTO> selectChangePage(ShopInfoAuditBO bo) {
        Page<ShopInfoAuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page.setRecords(baseMapper.selectChangePage(page, bo));
        return page;
    }

    @Override
    public Page<ShopInfoAuditDTO> selectAddBrandPage(ShopInfoAuditBO bo) {
        Page<ShopInfoAuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page.setRecords(baseMapper.selectAddBrandPage(page, bo));
        return page;
    }

    @Override
    public Page<ShopInfoAuditDTO> selectAddCategoryPage(ShopInfoAuditBO bo) {
        Page<ShopInfoAuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page.setRecords(baseMapper.selectAddCategoryPage(page, bo));
        return page;
    }

    @Override
    public ShopInfoAuditLoginVO getLogin(Long id) {
        ShopInfoAuditDTO dto = this.selectById(id);
        ShopInfoAuditLoginVO vo = new ShopInfoAuditLoginVO();
        BeanUtils.copyProperties(dto,vo);
        //联系人
        UserInfoDTO userInfoDTO = iUserInfoService.selectById(vo.getUserId());
        vo.setContactName(userInfoDTO.getContactName());
        //手机邮箱
        Wrapper<UserLoginDTO> loginWrapper = new EntityWrapper<>();
        loginWrapper.eq("user_id", vo.getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(loginWrapper);
        vo.setMobile(userLoginDTO.getMobile());
        vo.setEmail(userLoginDTO.getEmail());
        //资质
        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", vo.getUserId());
        wrapper.eq("shop_audit_id", vo.getId());
        wrapper.eq("audit_type", ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode());//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectList(wrapper);
        vo.setShopQualificationAuditDTOList(shopQualificationAuditDTOList);
        return vo;
    }

    @Override
    //@LcnTransaction
    public boolean loginReturn(ShopInfoAuditLoginReturnBO returnBO) {
        //debug预留事务
        /**
         * 更新店铺审核表
         */
        ShopInfoAuditDTO shopInfoAuditDTO = new ShopInfoAuditDTO();
        BeanUtils.copyProperties(returnBO,shopInfoAuditDTO);
        shopInfoAuditDTO.setId(returnBO.getId());
        shopInfoAuditDTO.setUpdateUserId(returnBO.getCreateUserId());
        shopInfoAuditDTO.setStatus(ShoppingCodeEnum.AUDIT_RESUBMIT.getCode());//0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        this.updateById(shopInfoAuditDTO);

        /**
         * 增加审核日志表
         */
        ShopInfoAuditLogDTO shopInfoAuditLogDTO = new ShopInfoAuditLogDTO();
        shopInfoAuditLogDTO.setId(ymlConfig.getPkId());
        shopInfoAuditLogDTO.setShopAuditId(returnBO.getId());
        shopInfoAuditLogDTO.setCreateUserId(returnBO.getCreateUserId());
        shopInfoAuditLogDTO.setUserId(returnBO.getCreateUserId());
        shopInfoAuditLogDTO.setType(shopInfoAuditDTO.getType());
        shopInfoAuditLogDTO.setStatus(ShoppingCodeEnum.AUDIT_CREATE.getCode());
        iShopInfoAuditLogService.insert(shopInfoAuditLogDTO);

        /**
         * 更新资质审核表
         */
        List<ShopQualificationAuditSaveBO> shopQualificationAuditSaveBOList = JSON.parseArray(returnBO.getShopQualificationAuditSaveBOList(), ShopQualificationAuditSaveBO.class);
        for (ShopQualificationAuditSaveBO bo : shopQualificationAuditSaveBOList){
            ShopQualificationAuditDTO qualificationAuditDTO = new ShopQualificationAuditDTO();
            qualificationAuditDTO.setId(bo.getId());
            qualificationAuditDTO.setFileUrl(bo.getFileUrl());
            qualificationAuditDTO.setBeginDate(bo.getBeginDate());
            qualificationAuditDTO.setEndDate(bo.getEndDate());
            qualificationAuditDTO.setStatus(ShoppingCodeEnum.AUDIT_CREATE.getCode());//是否审核 0未审核、1打回、2审核通过、3审核失败
            iShopQualificationAuditService.updateMay(qualificationAuditDTO);
        }

        /**
         * 更新用户状态及名称
         */
        UserInfoDTO userInfoDTO = iUserInfoService.selectById(returnBO.getCreateUserId());
        UserInfoDTO userDTO = new UserInfoDTO();
        userDTO.setId(userInfoDTO.getId());
        userDTO.setName(returnBO.getName());
        userDTO.setContactName(returnBO.getContactName());
        userDTO.setStatus(0);//0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
        return iUserInfoService.updateById(userDTO);
    }

    @Override
    public ShopInfoAuditOpenVO getOpen(Long id) {
        ShopInfoAuditOpenVO vo = new ShopInfoAuditOpenVO();
        ShopInfoAuditDTO dto = this.selectById(id);
        if (dto == null ){
            return vo;
        }

        //填充基础信息
        vo.setId(id);
        vo.setCreateUserId(dto.getCreateUserId());
        vo.setUpdateUserId(dto.getUpdateUserId());
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setShopId(dto.getShopId());
        vo.setName(dto.getName());
        vo.setDomain(dto.getDomain());
        vo.setShopType(dto.getShopType());
        vo.setShopName(dto.getShopName());
        vo.setShopRealName(dto.getShopRealName());
        vo.setShopAbbreviation(dto.getShopAbbreviation());
        vo.setBankName(dto.getBankName());
        vo.setBankCardNo(dto.getBankCardNo());
        vo.setBankAddress(dto.getBankAddress());
        vo.setContent(dto.getContent());

        //可售区域
        ShopSalesAreaBO shopSalesAreaBO = new ShopSalesAreaBO();
        shopSalesAreaBO.setShopId(dto.getShopId());
        List<ShopSalesAreaVO> shopSalesAreaVOS =  shopSalesAreaService.queryList(shopSalesAreaBO);
        vo.setShopSalesAreaVOS(shopSalesAreaVOS);

        //基础资质
        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", id);
        wrapper.eq("audit_type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        wrapper.andNew().eq("type", 1).or().eq("type", 2); //0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectList(wrapper);
        vo.setBaseQualificationAuditDTOList(shopQualificationAuditDTOList);

        /**
         * 品牌资质
         */
        //品牌关系审核表
        Wrapper<ShopBrandRelationAuditDTO> bwrapper = new EntityWrapper<>();
        bwrapper.eq("shop_audit_id", id);
        bwrapper.eq("type", 1);//类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        List<ShopBrandRelationAuditDTO> shopBrandRelationAuditDTOList = iShopBrandRelationAuditService.selectList(bwrapper);

        ArrayList<ShopInfoAuditBrandVO> shopInfoAuditBrandVOList = new ArrayList<ShopInfoAuditBrandVO>();
        for (ShopBrandRelationAuditDTO shopBrandRelationAuditDTO : shopBrandRelationAuditDTOList){
            ShopInfoAuditBrandVO shopInfoAuditBrandVO = new ShopInfoAuditBrandVO();
            Long brandId = shopBrandRelationAuditDTO.getBrandId();
            shopInfoAuditBrandVO.setShopCategoryRelationAuditId(shopBrandRelationAuditDTO.getId());
            /**
             * 品牌基础信息
             */
            //品牌是否存在 0存在。1不存在
            if (shopBrandRelationAuditDTO.getBrandType() == 0){
                BrandInfoDTO brandInfoDTO = iBrandInfoService.selectById(brandId);
                shopInfoAuditBrandVO.setBrandId(brandId);
                shopInfoAuditBrandVO.setBrandName(brandInfoDTO.getName());
                shopInfoAuditBrandVO.setType(0);
                shopInfoAuditBrandVO.setLogoUrl(brandInfoDTO.getLogoUrl());
            }else{
                BrandInfoAuditDTO brandInfoAuditDTO = iBrandInfoAuditService.selectById(brandId);
                shopInfoAuditBrandVO.setBrandId(brandId);
                shopInfoAuditBrandVO.setBrandName(brandInfoAuditDTO.getName());
                shopInfoAuditBrandVO.setType(1);
                shopInfoAuditBrandVO.setLogoUrl(brandInfoAuditDTO.getLogoUrl());
            }

            /**
             * 品牌下资质
             */
            Wrapper<ShopQualificationAuditDTO> bqwrapper = new EntityWrapper<>();
            bqwrapper.eq("shop_audit_id", id);
            bqwrapper.eq("brand_id", brandId);
            bqwrapper.eq("audit_type", 1);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
            //0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
            bqwrapper.andNew().eq("type", 3).or().eq("type", 4).or().eq("type", 5).or().eq("type", 6);
            List<ShopQualificationAuditDTO> bqAuditDTOList = iShopQualificationAuditService.selectList(bqwrapper);
            shopInfoAuditBrandVO.setBrandQualificationAuditDTOList(bqAuditDTOList);

            /**
             * 品牌类目
             */
            Wrapper<ShopCategoryRelationAuditDTO> scwrapper = new EntityWrapper<>();
            scwrapper.eq("shop_audit_id", id);
            scwrapper.eq("brand_id", brandId);
            scwrapper.eq("type", 1);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
            List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.selectList(scwrapper);
            shopInfoAuditBrandVO.setShopCategoryRelationAuditDTOList(shopCategoryRelationAuditDTOList);
            shopInfoAuditBrandVO.setCommodityCategoryTreeVOList(getCTree(shopCategoryRelationAuditDTOList));
            shopInfoAuditBrandVOList.add(shopInfoAuditBrandVO);
        }
        vo.setShopInfoAuditBrandVOList(shopInfoAuditBrandVOList);
        return vo;
    }

    @Override
    public boolean openReturn(ShopInfoAuditOpenReturnBO returnBO) {
        //debug预留事务
        /**
         * 更新店铺审核表
         */
        ShopReceiveCardSaveBO shopReceiveCardSaveBO = JSON.parseObject(returnBO.getShopReceiveCardSaveBO(), ShopReceiveCardSaveBO.class);
        ShopInfoAuditDTO shopInfoAuditDTO = new ShopInfoAuditDTO();
        ShopInfoAuditDTO oldDto = this.selectById(returnBO.getShopAuditId());

        shopInfoAuditDTO.setId(oldDto.getId());
        shopInfoAuditDTO.setShopName(returnBO.getShopName());//店铺名字
        shopInfoAuditDTO.setShopRealName(returnBO.getShopRealName());
        shopInfoAuditDTO.setShopAbbreviation(returnBO.getShopAbbreviation());
        shopInfoAuditDTO.setDomain(returnBO.getDomain());//二级域名
        shopInfoAuditDTO.setBankName(shopReceiveCardSaveBO.getBankName());//开户银行名称
        shopInfoAuditDTO.setBankCardNo(shopReceiveCardSaveBO.getBankCardNo());//开户卡号
        shopInfoAuditDTO.setBankAddress(shopReceiveCardSaveBO.getBankAddress());//开户行地址
        shopInfoAuditDTO.setStatus(4);//0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        this.updateById(shopInfoAuditDTO);

        //更新店铺表
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(oldDto.getShopId());
        shopInfoDTO.setSellerStatus(0);//开店审核状态 0创建、1审核通过、2审核打回、3，审核不通过。4重新提交。5.未提交
        iShopInfoService.updateById(shopInfoDTO);

        /**
         * 增加审核日志表
         */
        ShopInfoAuditLogDTO shopInfoAuditLogDTO = new ShopInfoAuditLogDTO();
        shopInfoAuditLogDTO.setId(ymlConfig.getPkId());
        shopInfoAuditLogDTO.setShopAuditId(oldDto.getId());
        shopInfoAuditLogDTO.setShopId(oldDto.getShopId());
        shopInfoAuditLogDTO.setCreateUserId(returnBO.getCreateUserId());
        shopInfoAuditLogDTO.setType(shopInfoAuditDTO.getType());
        shopInfoAuditLogDTO.setStatus(0);
        iShopInfoAuditLogService.insert(shopInfoAuditLogDTO);

        /**
         * 更新资质表
         */
        if (returnBO.getShopQualificationAuditDTOList() != null && !"".equals(returnBO.getShopQualificationAuditDTOList())){
            List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = JSON.parseArray(returnBO.getShopQualificationAuditDTOList(), ShopQualificationAuditDTO.class);
            for (ShopQualificationAuditDTO shopQualificationAuditDTO : shopQualificationAuditDTOList){
                ShopQualificationAuditDTO newQualificationAuditDTO = new ShopQualificationAuditDTO();
                BeanUtils.copyProperties(shopQualificationAuditDTO, newQualificationAuditDTO);
                newQualificationAuditDTO.setUpdateUserId(returnBO.getCreateUserId());
                newQualificationAuditDTO.setStatus(0);//审核状态 0审核中，1审核通过，2审核打回，3审核不通过
                iShopQualificationAuditService.updateMay(newQualificationAuditDTO);
            }
        }

        /**
         * 更新品牌分类表
         * 1.删除原有品牌分类表
         * 2.新增现有品牌分类表
         */
        //删除原有品牌分类IDS
        if (returnBO.getRelationAuditIds() != null && !"".equals(returnBO.getRelationAuditIds())){
            String[] ids = returnBO.getRelationAuditIds().split(",");
            iShopCategoryRelationAuditService.deleteBatchIds(Arrays.asList(ids));
        }

        //新增现有品牌分类表
        if (returnBO.getShopCategoryRelationAuditDTOList() != null && !"".equals(returnBO.getShopCategoryRelationAuditDTOList())){
            List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = JSON.parseArray(returnBO.getShopCategoryRelationAuditDTOList(), ShopCategoryRelationAuditDTO.class);
            for (ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO : shopCategoryRelationAuditDTOList){
                shopCategoryRelationAuditDTO.setId(ymlConfig.getPkId());
                shopCategoryRelationAuditDTO.setStatus(0);
                iShopCategoryRelationAuditService.insert(shopCategoryRelationAuditDTO);
            }
        }
        return true;
    }

    @Override
    public ShopInfoAuditChangeVO getChange(Long id) {
        ShopInfoAuditChangeVO vo = new ShopInfoAuditChangeVO();
        ShopInfoAuditDTO shopInfoAuditDTO = this.selectById(id);
        vo.setId(id);
        vo.setCreateUserId(shopInfoAuditDTO.getCreateUserId());
        vo.setName(shopInfoAuditDTO.getName());
        vo.setShopName(shopInfoAuditDTO.getShopName());
        vo.setShopId(shopInfoAuditDTO.getShopId());
        vo.setIdCardType(shopInfoAuditDTO.getIdCardType());

        //获取待审资质列表
        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", id).eq("audit_type", 2);
        vo.setShopQualificationAuditDTOList(iShopQualificationAuditService.selectList(wrapper));
        return vo;
    }

    @Override
    public ShopInfoAuditNewBrandVO getBrand(Long id) {
        ShopInfoAuditNewBrandVO vo = new ShopInfoAuditNewBrandVO();
        ShopInfoAuditDTO dto = this.selectById(id);
        if (dto == null ){
            return vo;
        }

        //填充基础信息
        vo.setId(id);
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setName(dto.getName());
        vo.setShopName(dto.getShopName());
        vo.setShopId(dto.getShopId());

        /**
         * 品牌资质
         */
        //品牌关系审核表
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", id);
        wrapper.eq("type", 3);//类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectOne(wrapper);

        Long brandId = shopBrandRelationAuditDTO.getBrandId();

        /**
         * 品牌基础信息
         */
        //品牌是否存在 0存在。1不存在
        if (shopBrandRelationAuditDTO.getBrandType() == 0){
            BrandInfoDTO brandInfoDTO = iBrandInfoService.selectById(brandId);
            vo.setBrandId(brandId);
            vo.setBrandName(brandInfoDTO.getName());
            vo.setType(0);
            vo.setLogoUrl(brandInfoDTO.getLogoUrl());
        }else{
            BrandInfoAuditDTO brandInfoAuditDTO = iBrandInfoAuditService.selectById(brandId);
            vo.setBrandId(brandId);
            vo.setBrandName(brandInfoAuditDTO.getName());
            vo.setType(1);
            vo.setLogoUrl(brandInfoAuditDTO.getLogoUrl());
        }

        /**
         * 品牌下资质
         */
        Wrapper<ShopQualificationAuditDTO> bqwrapper = new EntityWrapper<>();
        bqwrapper.eq("shop_audit_id", id);
        bqwrapper.eq("brand_id", brandId);
        bqwrapper.eq("audit_type", 3);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        //0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        bqwrapper.andNew().eq("type", 3).or().eq("type", 4).or().eq("type", 5).or().eq("type", 6);
        List<ShopQualificationAuditDTO> bqAuditDTOList = iShopQualificationAuditService.selectList(bqwrapper);
        vo.setBrandQualificationAuditDTOList(bqAuditDTOList);

        /**
         * 品牌类目
         */
        Wrapper<ShopCategoryRelationAuditDTO> scwrapper = new EntityWrapper<>();
        scwrapper.eq("shop_audit_id", id);
        scwrapper.eq("brand_id", brandId);
        scwrapper.eq("type", 3);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.selectList(scwrapper);
        vo.setShopCategoryRelationAuditDTOList(shopCategoryRelationAuditDTOList);
        vo.setCommodityCategoryTreeVOList(getCTree(shopCategoryRelationAuditDTOList));
        return vo;
    }

    @Override
    public ShopInfoAuditCategoryVO getCategory(Long id) {
        ShopInfoAuditCategoryVO vo = new ShopInfoAuditCategoryVO();
        ShopInfoAuditDTO dto = this.selectById(id);
        if (dto == null ){
            return vo;
        }

        //填充基础信息
        vo.setId(id);
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setName(dto.getName());
        vo.setShopName(dto.getShopName());
        vo.setShopId(dto.getShopId());

        //品牌关系审核表
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", id);
        wrapper.eq("type", 4);//类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectOne(wrapper);
        vo.setShopCategoryRelationAuditId(shopBrandRelationAuditDTO.getId());
        BrandInfoDTO brandInfoDTO = iBrandInfoService.selectById(shopBrandRelationAuditDTO.getBrandId());
        vo.setBrandId(shopBrandRelationAuditDTO.getBrandId());
        vo.setBrandName(brandInfoDTO.getName());
        vo.setLogoUrl(brandInfoDTO.getLogoUrl());

        /**
         * 品牌类目
         */
        Wrapper<ShopCategoryRelationAuditDTO> scwrapper = new EntityWrapper<>();
        scwrapper.eq("shop_audit_id", id);
        scwrapper.eq("brand_id", shopBrandRelationAuditDTO.getBrandId());
        scwrapper.eq("type", 4);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.selectList(scwrapper);
        vo.setShopCategoryRelationAuditDTOList(shopCategoryRelationAuditDTOList);
        vo.setCommodityCategoryTreeVOList(getCTree(shopCategoryRelationAuditDTOList));
        return vo;
    }

    private List<CommodityCategoryTreeVO> getCTree(List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList){
        List<CommodityCategoryTreeVO> root = new ArrayList<>();
        for (ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO : shopCategoryRelationAuditDTOList) {
            List<CommodityCategoryTreeVO> byChildId = this.getByChildId(shopCategoryRelationAuditDTO.getCategoryId(), shopCategoryRelationAuditDTO.getStatus(), new ArrayList<>());
            root.addAll(byChildId);
        }
        //查询最顶级的类目
        Wrapper<CommodityCategoryDTO> wrapperParent = new EntityWrapper<>();
        wrapperParent.eq("is_publish", 1).eq("status", 0)
                .eq("parent_id", 0).orderBy("sort", true);
        List<CommodityCategoryDTO> parentList = iCommodityCategoryService.selectList(wrapperParent);
        for (CommodityCategoryDTO commodityCategoryDTO : parentList) {
            CommodityCategoryTreeVO commodityCategoryTreeVO = new CommodityCategoryTreeVO();
            BeanUtils.copyProperties(commodityCategoryDTO, commodityCategoryTreeVO);
            root.add(commodityCategoryTreeVO);
        }
        //去重
        List<CommodityCategoryTreeVO> realRoot = root.stream().distinct().collect(Collectors.toList());
        //构建树形结构
        List<CommodityCategoryTreeVO> list = this.builTree(realRoot);
        List<CommodityCategoryTreeVO> real = new ArrayList<>();
        for (CommodityCategoryTreeVO categoryTreeVO : list) {
            real.addAll(categoryTreeVO.getChildList());
        }

        return real.stream().distinct().collect(Collectors.toList());
    }

    /**
     * description: 建立树形结构
     * param: [menuList]
     * author: chenyu.wang
     * date: 2020-07-28
     * return: java.util.List<com.ztgf.commodity.models.vo.CommodityCategoryTreeVO>
     */
    public List<CommodityCategoryTreeVO> builTree(List<CommodityCategoryTreeVO> menuList) {
        List<CommodityCategoryTreeVO> treeMenus = new ArrayList<>();
        for (CommodityCategoryTreeVO menuNode : getRootNode(menuList)) {
            menuNode = this.buildChilTree(menuNode, menuList);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    /**
     * description: 递归，建立子树形结构
     * param: [pNode, menuList]
     * author: chenyu.wang
     * date: 2020-07-28
     * return: com.ztgf.commodity.models.vo.CommodityCategoryTreeVO
     */
    private CommodityCategoryTreeVO buildChilTree(CommodityCategoryTreeVO pNode, List<CommodityCategoryTreeVO> menuList) {
        List<CommodityCategoryTreeVO> chilMenus = new ArrayList<>();
        for (CommodityCategoryTreeVO menuNode : menuList) {
            if (menuNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(this.buildChilTree(menuNode, menuList));
            }
        }
        pNode.setChildList(chilMenus);
        return pNode;
    }

    /**
     * description: 获取根节点
     * param: [menuList]
     * author: chenyu.wang
     * date: 2020-07-28
     * return: java.util.List<com.ztgf.commodity.models.vo.CommodityCategoryTreeVO>
     */
    private List<CommodityCategoryTreeVO> getRootNode(List<CommodityCategoryTreeVO> menuList) {
        List<CommodityCategoryTreeVO> rootMenuLists = new ArrayList<>();
        for (CommodityCategoryTreeVO menuNode : menuList) {
            if (menuNode.getParentId().intValue() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

    /**
     * description: 根据最终极类目id查询所有上级list
     * param: [id, list]
     * author: chenyu.wang
     * date: 2020-07-28
     * return: java.util.List<com.ztgf.commodity.models.vo.CommodityCategoryTreeVO>
     */
    public List<CommodityCategoryTreeVO> getByChildId(Long id, Integer status, List<CommodityCategoryTreeVO> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        CommodityCategoryDTO dto = iCommodityCategoryService.selectById(id);
        if (dto == null) {
            return null;
        }
        if (dto.getParentId() == 0) {
            return list;
        } else {
            CommodityCategoryTreeVO treeVO = new CommodityCategoryTreeVO();
            treeVO.setId(dto.getId());
            treeVO.setName(dto.getName());
            treeVO.setInitials(dto.getInitials());
            treeVO.setParentId(dto.getParentId());
            treeVO.setSort(dto.getSort());
            treeVO.setCode(dto.getCode());
            treeVO.setStatus(status);
            list.add(treeVO);
        }
        return this.getByChildId(dto.getParentId(), status, list);
    }
}
